# -*- coding: utf-8 -*-
# -----------------------------------------
# @Time       : 2020/9/29 17:29
# @Author     : dqz 
# @Email      : 1513727379@qq.com
# @File       : controller.py
# @Software   : PyCharm
# -----------------------------------------
import argparse
from binlog2sql.binglog2sql import Binlog2sql
from pymysqlreplication.row_event import DeleteRowsEvent, WriteRowsEvent, UpdateRowsEvent

map_event = {
    "INSERT": WriteRowsEvent,
    "DELETE": DeleteRowsEvent,
    "UPDATE": UpdateRowsEvent
}


def parse_args():
    parser = argparse.ArgumentParser(description='Parse MySQL binlog to SQL you want', add_help=False)
    connect_setting = parser.add_argument_group('connect setting')
    connect_setting.add_argument('-h', '--host', dest="host", type=str, help="mysql database server host",
                                 default='127.0.0.1')
    connect_setting.add_argument('-u', '--user', dest='user', type=str, help='mysql database server user',
                                 default='root')
    connect_setting.add_argument('-p', '--passwd', dest='passwd', type=str, nargs="*",
                                 help='mysql database server passwd', default='')
    connect_setting.add_argument('-P', '--port', dest='port', type=int, help='mysql databvase server port',
                                 default=3306)
    ctl_setting = parser.add_argument_group("controller info")
    ctl_setting.add_argument('--start-file', dest='start_file', type=str, help="start binlog file to be parsed",
                             default=None)
    ctl_setting.add_argument('--end-file', dest='end_file', type=str, help="end binlog file parsed", default=None)
    ctl_setting.add_argument('--start-pos', dest='start_pos', type=int,
                             help='start parsed position of the binlog event', defult=4)
    ctl_setting.add_argument('--end-pos', dest='end_pos', type=int, help='stop parsed position of the binlog event',
                             default=None)
    ctl_setting.add_argument('--start-time', dest='start_time', type=str,
                             help="start time: format %%Y-%%m-%%d %%H:%%M:%%S", default='')
    ctl_setting.add_argument('--stop-time', dest='stop_time', type=str,
                             help='stop time: format %%Y-%%m-%%d %%H:%%M:%%S', default='')
    ctl_setting.add_argument('--primary-key', dest='primary_key', action="store_true",
                             help="insert sql has primary key", default=True)
    ctl_setting.add_argument('--rollback', dest="rollback", action='store_true', help="generate rollback sql",
                             default=False)
    ctl_setting.add_argument("--auto-position", dest="auto_position", type=str,
                             help="auto position: true, get current pos", default=None)
    ctl_setting.add_argument("--auto-stop", dest="auto_stop", action="store_true", help="stop parsed, when to exec pos",
                             default=False)
    event_setting = parser.add_argument_group("event control")
    event_setting.add_argument('--only-dml', dest='only_dml', action='store_true', help='only print dml', default=False)
    event_setting.add_argument("--only-events", dest='only_events', type=str, nargs="*",
                               default=["INSERT", "DELETE", "UPDATE"], help='sql type: support INSERT,DELETE,UPDATE')
    event_setting.add_argument("--only-schemas", dest="only_schemas", type=str, nargs="*", default='',
                               help="process databases")
    event_setting.add_argument("--ignore-schemas", dest="ignore_schemas", type=str, nargs="*", default='',
                               help='ignore process databases')
    event_setting.add_argument('--only-tables', dest='only_tables', type=str, nargs="*", default='',
                               help='only exists only_schema')
    parser.add_argument("--output-update-diff", dest="output_update_diff", action="store_true",
                        help="output update diff", default=False)
    parser.add_argument("--blocking", dest="blocking", action="store_true", help="Read on stream is blocking",
                        default=True)
    args = parser.parse_args()

    start_file = args.start_file
    end_file = args.end_file
    start_time = args.start_time
    stop_time = args.stop_time
    start_pos = args.start_pos
    end_pos = args.end_pos
    primary_key = args.primary_key
    rollback = args.rollback
    auto_stop = args.auto_stop
    only_dml = args.only_dml
    only_events = [map_event.get(event.upper()) for event in args.only_events]
    only_schemas = args.only_schemas
    only_tables = args.only_tables
    output_update_diff = args.output_update_diff
    binlog = Binlog2sql(start_file=start_file, end_file=end_file, start_time=start_time, stop_time=stop_time,
                        start_pos=start_pos, end_pos=end_pos, primary_key=primary_key, rollback=rollback,
                        auto_stop=auto_stop,only_dml=only_dml, only_events=only_events, only_schemas=only_schemas,
                        only_tables=only_tables,output_update_diff=output_update_diff)
    binlog.start_process_event()


def run():
    # conn=REPLICATION_SETTINGS.get("connection_settings")
    # binlog = Binlog2sql(start_file="mysql-bin.000100", start_time="2020-12-15 16:08:20",
    #                     stop_time="2021-12-10 08:57:30", only_dml=False, output_update_diff=True, rollback=True,
    #                     only_schemas=["test_qingdao_base","test_qingdao_biz","test_qingdao_stock"])
    # binlog = Binlog2sql(start_file="mysql-bin.000101", start_pos=26889054,
    #                     stop_time="2021-12-10 08:57:30", only_dml=False, output_update_diff=True, rollback=True,
    #                     only_schemas=["test_global","test_qingdao_base", "test_qingdao_biz", "test_qingdao_stock"])
    # binlog = Binlog2sql(start_file="mysql-bin.000054", start_pos=977872459,
    #                     stop_time="2021-12-10 08:57:30", only_dml=False, output_update_diff=True, rollback=True,
    #                     only_schemas=["test_global", "test_qingdao_base", "test_qingdao_biz", "test_qingdao_stock"])

    binlog = Binlog2sql(start_file="mysql-bin.000055", start_pos=424422,
                        stop_time="2021-12-10 08:57:30", only_dml=False, output_update_diff=True, rollback=False,
                        blocking=True)

    binlog.start_process_event()


if __name__ == "__main__":
    run()
